package com.compuvest.tld;

import javax.servlet.http.HttpServletResponse;
    
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import java.util.ArrayList;

import com.compuvest.web.Item;
import com.compuvest.web.Items;

public class Itemstld extends TagSupport {
/*
    public final static String STOCKFORM = "<b>&#x95; Stock: <font color=#808080>%s</font></b><br/>";
    public final static String PRICEFORM = "<div class=price>$%,.2f</div>";
    public final static String RESPRICEFORM = "<div class=prices>$%,.2f</div>";
    public final static String ADDBASKET = "%s?iidd=%d";
    public final static String BANNER = "<div class=mes2>%s</div>";
    public final static String URLSPEC = "<a href='javascript:void(0);' onClick=\"javascript:window.open('http://%s/info/SpecialPrice.html','','width=500,height=210,left=5,top=5');\">Special Price</a>";
    public final static String PRICERES = "<span class=rpr>$%,.2f</span>";
    public final static String IMGURL = "%s%s.jpg";
*/
    public final static String IMGURL = "%s%s.jpg";
    public final static String PRICEFORM = "$%,.2f";
    public final static String ADDBASKET = "%s?iidd=%d";

    protected int categoryID;
    protected int subcategoryID;
    protected int pageNo;
    protected String pageUrl;
    protected int sortID;
    protected boolean reseller = false;
    protected boolean tail = true;
    protected Items items;
    protected StringBuilder body;

//----------------------------
//
    @Override
    public int doStartTag() throws JspException {

	if(items == null) return SKIP_BODY;

	ArrayList<Item> itemsArray = items.getItems();

	if(items.size() != 0) {
	    body = new StringBuilder(25000);
	    body.append("<table id='cv_it_h'>");
            body.append("<colgroup><col width='70%'><col width='30%'></colgroup>");
            body.append("<tr>");
            body.append("<th>P r o d u c t&nbsp; &nbsp;N a m e</th>");
            body.append("<th align=right>Sort by : ");
            body.append("<select name='srt' onChange='document.SortForm.submit();return false;'>");
            body.append("<option value=1 ").append(sortID == 1 ? "selected=selected " : "").append("/>Price: lowest first");
            body.append("<option value=2 ").append(sortID == 2 ? "selected=selected " : "").append("/>Price: highest first");
            if(items.haveHotDeals()) {
                body.append("<option value=3 ").append(sortID == 3 ? "selected=selected " : "").append("/>Hot Deals: lowest");
                body.append("<option value=4 ").append(sortID == 4 ? "selected=selected " : "").append("/>Hot Deals: highest");
            }
            body.append("</select>");
            body.append("</th></tr></table>\n");

            String serverName = pageContext.getServletContext().getInitParameter("http");

            body.append("<table id='cv_ittb'>\n");
            body.append("<colgroup>");
            body.append("<col width='5%'><col width='0*'></colgroup>\n");
            for(Item item : itemsArray) {
                String imgn = pageContext.getServletContext().getInitParameter("imgn");

                body.append("<tr>");
                body.append("<td style='background:white;'>");
                body.append("<p><img id='rs' onError='hideErr(this);' onMouseout='resetpic();return false;' src='");
                body.append(String.format(IMGURL, imgn, item.getAccwareCode())).append("' onMouseover=\"cltip('");
                body.append(String.format(IMGURL, imgn, item.getAccwareCode())).append("');\"/></p></td>");

                body.append("<td class='tbg'>\n");
                body.append("<table id='cv_ittr'>\n");
                body.append("<colgroup>");
                body.append("<col width='15%'><col width='15%'><col width='25%'><col width='30%'><col width='15%'>");
                body.append("</colgroup>");
                body.append("<tr><td colspan=6>");
                body.append("<a href='").append(serverName).append("Descr.jsp?iid=").append(item.getItemID()).append("'>").append(item.getItemName()).append("</a>");
                body.append(items.getResellerPriceLine(item));
                body.append("</td>");
                body.append("</tr>");
                body.append("<tr>");
                body.append("<td><li/>SKU: ").append(item.getAccwareCode()).append("</td>");
                body.append("<td><li/>").append(item.getCondition()).append("</td>");
                body.append("<td><li/>Stock: ");
                body.append(item.getStock() ? "Yes" : String.valueOf(item.getQuantity()));
                body.append("</td><td>&nbsp;</td>");
                body.append("<td><div id='sbtn'><a href='").append(String.format(ADDBASKET, serverName + "Basket.jsp", item.getItemID()));
                body.append("'><div>Add to Cart</div></a></div></td>");
                body.append("</tr></table>");
                body.append("</td>");
                body.append("</tr>\n");
	    }
	    body.append("</table>\n");
	    body.append("<span id='pix'></span>");

	    // make page number
//---------
	    StringBuilder url = new StringBuilder(150).append(pageUrl).append("&srt=").append(sortID);
	    int pages = items.getPages();
	    if(pages != 1) {

		int page = pageNo + 1;
		int start = 1;
		int end = pages;
		boolean offSetEnd = false;
	
		body.append("<p class='pg_cn'>Page:&nbsp;&nbsp;");
		if(pages - page > 9) {
		    end = page + 9;
		    offSetEnd = true;
		}
		if(page != 1)
		    body.append("<a href='").append(url).append("&pg=").append(page-2).append("'>&laquo;</a> ");
		if(page > 10) {
		    start = page - 9;
                    body.append("<a href='").append(url).append("&pg=0'>1</a> . . . ");
		}
		for(int i = start; i <= end; i++) {
		    if(i == page)
                        body.append("<font size=3>").append(i).append("</font> ");
		    else
    			body.append(" <a href='").append(url).append("&pg=").append(i-1).append("'>").append(i).append("</a> ");
		}
		if(offSetEnd)
		    body.append(" . . . <a href='").append(url).append("&pg=").append(pages-1).append("'>").append(pages).append("</a> ");
		if(page != pages)
                    body.append("<a href='").append(url).append("&pg=").append(page).append("'> &raquo;</a>");
                body.append("</p>");
	    }
            body.append("<br/>");
//---------
	    // save page link
	    pageContext.setAttribute("backItemsPage", url.append("&pg=").append(pageNo).toString());
	}
	else {
	    body = new StringBuilder(55);
	    if(tail)
                body.append("<h3>There were no matching result for your query.</h3>");
	}
        try {
	    pageContext.getOut().print(body.toString());
//	    pageContext.getOut().print("l="+body.length()+"<br/>"+body.toString());
        }
        catch(java.io.IOException e) {
            throw new JspTagException("Itemstld: " + e.getMessage());
        }
        return SKIP_BODY;
    }

//----------------------------
//
    @Override
    public int doEndTag() {
        return EVAL_PAGE;
    }

//----------------------------
//
    @Override
    public void release() {
        pageUrl = null;
        body = null;
        items = null;
    }

//----------------------------
//
    public void setCategoryID(int categoryID)       { this.categoryID = categoryID; }
    public void setSubcategoryID(int subcategoryID) { this.subcategoryID = subcategoryID; }
    public void setPageNo(int pageNo)               { this.pageNo = pageNo; }
    public void setPageUrl(String pageUrl)          { this.pageUrl = pageUrl; }
    public void setReseller(boolean reseller)       { this.reseller = reseller; }
    public void setItems(Items items)               { this.items = items; }
    public void setSortID(int sortID)               { this.sortID = sortID; }
    public void setTailShow(boolean tail)           { this.tail = tail; }
}
